package pv.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import pv.finder.R;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public class Utility {

	public static void showMessage(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	public static void LogM(Class c, String msg) {
		if (msg != null && !msg.isEmpty())
			Log.i("unit test" + c, msg);
	}

	public static void LogE(Class c, Exception e) {
		if (e != null && e.getMessage() != null)
			Log.i("unit test" + c, e.getMessage());
	}

	public static void hideKeyboard(Context context) {
		try {
			InputMethodManager inputManager = (InputMethodManager) context
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager.hideSoftInputFromWindow(((Activity) context)
					.getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static void goToFragment(FragmentActivity fragmentActivity,
			Fragment fragment, String tagFragmentCurrent, String tagFragmentNew) {

		FragmentManager fm = fragmentActivity.getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		if (isAnimation()) {
		}
		if (tagFragmentCurrent == null)
			fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

		ft.replace(getFragmentContentView(), fragment, tagFragmentNew);
		ft.addToBackStack(tagFragmentNew);
		ft.commit();
		hideKeyboard(fragmentActivity);
	}

	public static boolean isAnimation() {
		return false;
	}

	public static void goToFragment(FragmentActivity fragmentActivity,
			Fragment sherlockFragment, String tagFragmentNew) {
		goToFragment(fragmentActivity, sherlockFragment, null, tagFragmentNew);
		return;
	}

	public static String readStream(InputStream in) throws IOException {
		int version = 1;
		switch (version) {
		case 0:

			BufferedReader reader = null;
			String result = null;
			try {
				reader = new BufferedReader(new InputStreamReader(in));
				result = "";
				String line = "";
				while ((line = reader.readLine()) != null) {
					result += line;
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();

					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return result;

		default:
			StringBuilder sb = new StringBuilder();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in));
			String line;
			while ((line = rd.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();

		}

	}

	/*
	 * delete file from sdcard
	 */
	public static void deleteFile(String filePath) {
		try {
			File file = new File(filePath);
			file.delete();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static String getCurrDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		System.out.println("time => " + dateFormat.format(cal.getTime()));

		String time_str = dateFormat.format(cal.getTime());
		return time_str;
	}

	public static int getFragmentContentView() {
		return R.id.main_content;
	}
}
